package com.demo.controller;

import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StreamOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * test
 */
@RestController
public class TestController {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    // 测试点对点模式
    @GetMapping("/testQueue")
    public String testMq() {
        // 创建Stream
        StreamOperations streamOperations = redisTemplate.opsForStream();
        //生产者发送消息  "emailStream"-是消费者配置的Stream队列名称
        Map<String, String> map = new HashMap<>();
        map.put("test1", "hello world0!");

        // 发送两次testStream
        Map<String, String> map0 = new HashMap<>();
        map0.put("test1", "hello world1!");
        streamOperations.add("testDemoQueueStream", map0);
        return "success";
    }

    // 测试发布订阅模式
    @GetMapping("/testPublish")
    public String testMq2() throws InterruptedException {
        Map<String, String> map4 = new HashMap<>();
        map4.put("test4", "hello world4!");
        redisTemplate.convertAndSend("testDemoTopic", JSONUtil.toJsonStr(map4));
        return "success";
    }
}
